<template>
	<div class="layout-padding">
		<div class="layout-padding-auto layout-padding-view">
			<el-row shadow="hover" v-show="showSearch" class="ml10">
				<el-form :model="state.queryForm" ref="queryRef" :inline="true" @keyup.enter="getDataList">
					<el-form-item :label="t('aigcConversation.title')" prop="title">
						<el-input :placeholder="t('aigcConversation.inputTitleTip')" v-model="state.queryForm.title" />
					</el-form-item>
					<el-form-item>
						<el-button icon="search" type="primary" @click="getDataList">
							{{ t('common.queryBtn') }}
						</el-button>
						<el-button icon="Refresh" @click="resetQuery">{{ t('common.resetBtn') }}</el-button>
					</el-form-item>
				</el-form>
			</el-row>
			<el-row>
				<div class="mb8" style="width: 100%">
					<el-button icon="folder-add" type="primary" class="ml10" @click="conversationDialogRef.openDialog()" v-auth="'aigc_conversation_add'">
						{{ t('common.addBtn') }}
					</el-button>
					<el-button plain :disabled="multiple" icon="Delete" type="primary" class="ml10" v-auth="'aigc_conversation_delete'" @click="handleDelete(selectObjs)">
						{{ t('common.delBtn') }}
					</el-button>
					<right-toolbar
						v-model:showSearch="showSearch"
						:export="'aigc_conversation_export'"
						@exportExcel="exportExcel"
						class="ml10"
						style="float: right; margin-right: 20px"
						@queryTable="getDataList"
					></right-toolbar>
				</div>
			</el-row>
			<el-table
				:data="state.dataList"
				v-loading="state.loading"
				style="width: 100%"
				@selection-change="handleSelectionChange"
				border
				:cell-style="tableStyle.cellStyle"
				:header-cell-style="tableStyle.headerCellStyle"
			>
				<el-table-column type="selection" width="50" align="center" />
				<el-table-column prop="title" :label="t('aigcConversation.title')" show-overflow-tooltip></el-table-column>
				<el-table-column prop="lastTime" :label="t('aigcConversation.lastTime')" show-overflow-tooltip></el-table-column>
				<el-table-column :label="t('common.action')" width="250">
					<template #default="scope">
						<el-button text type="primary" icon="edit-pen" v-auth="'aigc_conversation_edit'" @click="conversationDialogRef.openDialog(scope.row.id)">
							{{t('common.editBtn')}}
						</el-button>
                        <el-button
                            text
                            type="primary"
                            icon="delete"
                            v-auth="'aigc_conversation_delete'"
                            @click="handleDelete([scope.row.id])"
                            >{{ t('common.delBtn') }}
                        </el-button>
					</template>
				</el-table-column>
			</el-table>
			<pagination @size-change="sizeChangeHandle" @current-change="currentChangeHandle" v-bind="state.pagination" />
		</div>
		<!-- 编辑、新增  -->
		<conversation-dialog ref="conversationDialogRef" @refresh="getDataList()" />
	</div>
</template>

<script setup lang="ts" name="aigcConversation">
import { useI18n } from 'vue-i18n';
import { delObj, pageList } from '/@/api/aigc/conversation';
import { useMessage, useMessageBox } from '/@/hooks/message';
import { BasicTableProps, useTable } from '/@/hooks/table';

// 引入组件
const ConversationDialog = defineAsyncComponent(() => import('./form.vue'));
const { t } = useI18n();

// 定义变量内容
const conversationDialogRef = ref();
const queryRef = ref();
const showSearch = ref(true);
// 多选rows
const selectObjs = ref([]) as any;
// 是否可以多选
const multiple = ref(true);

const state: BasicTableProps = reactive<BasicTableProps>({
	queryForm: {
		title: '',
	},
	pageList: pageList, // H
	descs: ['create_time'],
});

//  table hook
const { getDataList, currentChangeHandle, sizeChangeHandle, downBlobFile, tableStyle } = useTable(state);

// 清空搜索条件
const resetQuery = () => {
	queryRef.value.resetFields();
	getDataList();
};


// 多选事件
const handleSelectionChange = (objs: { id: string }[]) => {
	selectObjs.value = objs.map(({ id }) => id);
	multiple.value = !objs.length;
};

// 删除操作
const handleDelete = async (ids: string[]) => {
	try {
		await useMessageBox().confirm(t('common.delConfirmText'));
	} catch {
		return;
	}

	try {
		await delObj(ids);
		getDataList();
		useMessage().success(t('common.delSuccessText'));
	} catch (err: any) {
		useMessage().error(err.msg);
	}
};

 //导出
const exportExcel = ()=>{
    downBlobFile('/aigc/app/exportExcel',state.queryForm,'aigcConversation.xlsx');
}
</script>
